package com.hanqi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hanqi.dao.SysUserMapper;
import com.hanqi.entity.SysMenu;
import com.hanqi.dao.SysMenuMapper;
import com.hanqi.entity.SysUser;
import com.hanqi.service.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zjk
 * @since 2019-04-27
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    protected SysUserMapper sysUserMapper;

    @Override
    public List<SysMenu> listByUserId(String id) {
        SysUser user = sysUserMapper.selectById(id);
        if (user.getUserType() == 1) { // 超级管理员
            QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
            wrapper.eq("delete_flag", 0);
            return baseMapper.selectList(wrapper);
        }
        return baseMapper.listByUserId(id);
    }

    @Override
    public List<SysMenu> listAllByUserId(String id) {
        List<SysMenu> list = listByUserId(id);
        SysMenu sysMenu = SysMenu.builder()
                .id("0")
                .menuName("菜单")
                .build();
        list.add(sysMenu);
        return list;
    }

    @Override
    public List<Map<String, Object>> roleMenuData(String roleId) {
        List<Map<String, Object>> list = baseMapper.roleMenuData(roleId);
        for (Map<String, Object> map : list) {
            if (map.get("checked").equals("1")) {
                map.put("checked", true);
            } else {
                map.put("checked", false);
            }
        }
        return list;
    }
}
